const {
    mysql
  } = require('../../database/mysql')
  const Student = require('../../model/student')
  const Uuid = require('../../utils/uuid')
  
  class StudentService {
    //添加新球场
    static async newStudent({
      ctx
    }) {
      let newStudentInfo = {}
      newStudentInfo = ctx.request.body
      const student = new Student(newStudentInfo)
      student.id = new Uuid().uuid
      student.createdDate = new Date()
      const result = await mysql('student').insert(student.getData().student)
      return result[0] === 0 ? {
        result: true,
        student: student.getData().student
      } : {
        result: false
      }
    }
  
    //获取所有球场信息
    static async getAllStudent({ctx}) {
      let newStudentInfo = {}
      newStudentInfo = ctx.request.body
      const student = new Student(newStudentInfo)
      const result = await mysql('student').where(student.getData().studentWithNoNull).select().orderBy('created_date', 'desc')
      const StudentsInfo = []
      result.forEach((StudentInfo) => {
        StudentsInfo.push(StudentInfo)
      })

      let StudentsInfo2 = []
      for(let index  in StudentsInfo){
        const userinfo = await mysql('user').where({student_id:StudentsInfo[index].id}).select()
        StudentsInfo2.push({...StudentsInfo[index],userPhone:userinfo[0].phone||null})
      }

      return StudentsInfo2
    }

    // 根据id找用户
  static async getStudentById({ id }) {
    console.log(id)
  
      const [studentInfo] = await mysql('student')
        .where({ id })
        .select()
    if(studentInfo) {
      const student = new Student(studentInfo)
      return student.getData()
    }else{
      return false
    }
  }
  
      //获取所有球场信息(带订单)
      static async getStudentWithOrder({
        day1,day2,day3
     }) {
        const result = await mysql('student').select('student.*',
        {'day1':await mysql('order').select().where('student_id','=','student.id').andWhere({date:day1})
        },
        {'day2':
          await mysql('order').select().where('student_id','=','student.id').andWhere({date:day2})
        },
        {'day3':
          await mysql('order').select().where('student_id','=','student.id').andWhere({date:day3})
        }
        )
        const StudentsInfo = []
        result.forEach((StudentInfo) => {
          StudentsInfo.push(StudentInfo)
        })
        return StudentsInfo
      }
    // 删除某个球场
    static async delStudent({
      id
    }) {
      const result = await mysql('student')
        .where({
          id
        })
        .del()
  
      return result === 1
    }
    // 球场信息更新
    static async setStudent({
      ctx,
      id
    }) {
      let updateStudentInfo = {}
      updateStudentInfo = ctx.request.body
      const student = new Student(updateStudentInfo)
      const updateInfo = student.getData().studentWithNoNull
      Reflect.deleteProperty(updateInfo, 'id')
      Reflect.deleteProperty(updateInfo, 'created_date')
      Reflect.deleteProperty(updateInfo, 'updated_date')
      const result = await mysql('student').where({
        id
      }).update(updateInfo)
      return result === 1
    }
  }
  
  
  module.exports = StudentService